﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Project_1
{
    public partial class Form1 : Form
    {
        private double _add1 = 0;
        private double _add2 = 0;
        private string _oper = "";

        public double InputZone
        {
            get
            {
                if (string.IsNullOrEmpty(tbResult.Text)) return 0;
                else
                    return Convert.ToDouble(tbResult.Text);//Convert.ToDouble()代表将括号内的字符转换为双精度浮点数
            }

            set { tbResult.Text = value.ToString(); }
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void btnNum0_Click(object sender, EventArgs e)
        {
            tbResult.Text += ((Button)sender).Text;//tbResult.Text += "0";+=代表字符串连接，即原有1，+=0代表10
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            _add1 = InputZone;
            tbResult.Text = string.Empty; //string.Empty代表空字符串，类似""，但这种写法更规整
            _oper = "+";
        }

        private void btnSubtract_Click(object sender, EventArgs e)
        {
            _add1 = InputZone;
            tbResult.Text = string.Empty;
            _oper = "-";
        }

        private void btnMul_Click(object sender, EventArgs e)
        {
            _add1 = InputZone;
            tbResult.Text = string.Empty;
            _oper = "*";
        }

        private void btnDiv_Click(object sender, EventArgs e)
        {
            _add1 = InputZone;
            tbResult.Text = string.Empty;
            _oper = "/";
        }

        private void btnReciprocal_Click(object sender, EventArgs e)
        {
            _add1 = InputZone;
            tbResult.Text = (1.0 / _add1).ToString();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            tbResult.Text = string.Empty;
        }

        private void btnIndex_Click(object sender, EventArgs e)
        {
            _add1 = InputZone;
            tbResult.Text = string.Empty;
            _oper = "^";
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            if (tbResult.Text.Length <= 0)
                tbResult.Text = "";
            else
                tbResult.Text = tbResult.Text.Substring(0, tbResult.Text.Length - 1);
        }

        private void btnC_Click(object sender, EventArgs e)
        {
            tbResult.Text = string.Empty;
        }

        private void btnPercent(object sender, EventArgs e)
        {
            if (tbResult.Text.Length <= 0)
                tbResult.Text = "0";
            _add1 = Convert.ToDouble(tbResult.Text);
            tbResult.Text = string.Empty;
            _oper = "%";
        }

        private void btnCompute_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbResult.Text))
                return;
            {
                _add2 = Convert.ToDouble(tbResult.Text);
                switch (_oper)
                {
                    case "+"://加法运算
                        tbResult.Text = (_add1 + _add2).ToString();
                        break;
                    case "-"://减法运算
                        tbResult.Text = (_add1 - _add2).ToString();
                        break;
                    case "*"://乘法运算
                        tbResult.Text = (_add1 * _add2).ToString();
                        break;
                    case "/"://除法运算
                        tbResult.Text = (_add1 / _add2).ToString();
                        break;
                    case "%"://百分号
                        tbResult.Text = (_add1 * (_add2 / 100)).ToString();
                        break;
                    case "^"://指数
                        Double x = Math.Pow(_add1, _add2);
                        tbResult.Text = x.ToString();
                        break;
                    default:
                        break;
                }
            }

        }

        private void 复制CtrlCToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Clipboard.SetDataObject(tbResult.Text); 
        }

        private void 粘贴CtrlVToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IDataObject iData = Clipboard.GetDataObject();
            if (iData.GetDataPresent(DataFormats.Text))
            {
                tbResult.Text = (String)iData.GetData(DataFormats.Text);
            }

        }

        private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("^o^by●ヤ拽じ少鹰", "计算器V1.0", MessageBoxButtons.OK);
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {

        }

        private void NEW(object sender, EventArgs e)
        {
            tbResult.Text = "";
        }



    }
}
